//
//  YRVisitorLoginView.swift
//  YR 微博
//
//  Created by cyr on 15/10/9.
//  Copyright © 2015年 cyr. All rights reserved.
//

import UIKit

protocol YRVisitorLoginViewDelegate: NSObjectProtocol{
    
   func yrVisitorLoginViewWillLogin()
   func yrVisitorLoginViewWillRegister()
}

class YRVisitorLoginView: UIView {
    
  // 定义代理
    weak var delegate: YRVisitorLoginViewDelegate?
    
    func clickLogin(){
        
        delegate?.yrVisitorLoginViewWillLogin()
    }
    
    func clickRegister(){
        
        delegate?.yrVisitorLoginViewWillRegister()
    }
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    
    // MARK: - 设置视图信息
    // 创建一个方法用来设置不同界面的视图
    func setupViewInfo( isHome: Bool, imageName: String, message: String){
        
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        
        homeIconView.hidden = !isHome
        
        // 防止没个视图都背遮挡,判断是否是主页
        if !isHome
        {
            sendSubviewToBack(maskIconView)
        } else {
            startAnimation()
        }
    }
    
    // MARK: -首页动画
    private func startAnimation(){
        
        // 使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    //  设置界面
    private func setupUI(){
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 自动布局
        
        // 图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 小房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 消息文字
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 235))
        
        // 注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 登陆按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 遮罩的自动布局
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[Subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["Subview" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[Subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["Subview": maskIconView,"regButton": registerButton]))

        // 设置空白区域背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }

    
    // 懒加载控件
    private lazy var iconView: UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iv
        
    }()
    
    // 添加遮罩
    private lazy var maskIconView: UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return iv
        
    }()
    
    private lazy var homeIconView: UIImageView = {
       
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
        
    }()
    
    private lazy var messageLabel: UILabel = {
        let l = UILabel()
//        l.text = "读书越少对环境越不满；读书越多对自己越不满意。读书少的人，看问题越主观，越简单，越容易对什么都抱怨。"
        
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.lightGrayColor()
        
        l.numberOfLines = 0
        
        l.sizeToFit()
        return l
        
    }()
    
    private lazy var registerButton: UIButton = {
       let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    private lazy var loginButton: UIButton = {
       
        let btn = UIButton()
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
}
